<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
		<meta charset="utf-8" />
		<title>Auth</title>

		<meta name="description" content="with selectable elements and custom icons" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />

		<!-- bootstrap & fontawesome -->
		<link rel="stylesheet" href="../assets/css/bootstrap.min.css" />

		<!-- ace styles -->
		<link rel="stylesheet" href="../assets/css/ace.css" class="ace-main-stylesheet" id="main-ace-style" />

		<link rel="stylesheet" href="../assets/css/ace-skins.css" />
		<link rel="stylesheet" href="../assets/css/ace-rtl.css" />
		<link href="../assets/css/font-awesome.min.css" rel="stylesheet"/>
		<!-- ace settings handler -->
		<script src="../assets/js/ace-extra.js"></script>
		
		<style type="text/css">
			.black-cover{
				background: #000;
				opacity: 0.3;
				width: 100%;
				height: 100%;
				position: fixed;
				top: 0;	
				left: 0;
				z-index: 2000;
			}
			.checkAll{
				width: 15px;height: 15px;
				border-radius: 3px;		
				display: inline-block;
				position: absolute;
				top:10px;
				right: 30px;
				cursor: pointer;
			}
			.unCheck{
				background-color: #fafafa;
    			border: 1px solid #ccc;
			}
			.hadCheck{
				background: #f9a021;
				text-align: center;
				line-height: 15px;
				font-size: 12px;
				font-weight: bold;
			}
		</style>
	</head>

	<body class="no-skin" style="background:#fff">
		<div class="black-cover hidden"></div>
		<div style="margin: 10px 0;float: right;">
			<!--重置为初始化时的数据-->
			<button type="button" id="reset-btn" class="btn btn-danger" data-toggle="button" aria-pressed="false" autocomplete="off" style="margin-right: 20px;">重置</button>
			<!--提交后刷新页面-->
			<button type="button" id="submit-btn" class="btn btn-primary" data-toggle="button" aria-pressed="false" autocomplete="off" style="margin-right: 25px;">提交</button>

		</div>


		<div class="row" style="margin-left:0;margin-right:0;overflow-x:hidden;width: 100%;clear: both;">
			<div class="col-xs-12">
				<div class="row" style="margin-left:0;margin-right:0;overflow-x:hidden;">
					<div class="col-sm-6">
						<div class="widget-box widget-color-blue2">
							<div class="widget-header">
								<h4 class="widget-title lighter smaller">管理员权限管理</h4>
							</div>

							<div class="widget-body">
								<div class="widget-main padding-8">
									<ul id="tree1"></ul>
								</div>
							</div>
						</div>
					</div>

					<div class="col-sm-6">
						<div class="widget-box widget-color-green2">
							<div class="widget-header">
								<h4 class="widget-title lighter smaller">
									组件管理
								</h4>
								<span class="sellectAll-btn hidden" style="float: right;line-height:36px;margin-right:20px;position: relative;" >
									 <div class="checkAll unCheck"></div>全选
								</span>
							</div>

							<div class="widget-body">
								<div class="widget-main padding-8">
									<ul id="tree2" class="hidden"></ul>
								</div>
							</div>
						</div>			
					</div>
				</div>
			</div>
		</div>

		<!-- basic scripts -->
		<script src="../assets/js/jquery-1.10.1.min.js"></script>
		<script src="../assets/js/bootstrap.min.js"></script>
		<!-- page specific plugin scripts -->
		<script src="../assets/js/tree.js"></script>
		<script src="../assets/js/elements.treeview.js"></script>
		<script src="../assets/js/ace.js"></script>

	<script type="text/javascript">
	function showBlackCover(){
		$('.black-cover').removeClass('hidden');
	}
	
	function hideBlackCover(){
		$('.black-cover').addClass('hidden');
	}
	
	jQuery(function($){
		var _comArr = []; //保存获取的所有组件数据

		var sampleData = initiateDemoData();//see below

		$('#tree1').ace_tree({
			dataSource: sampleData['dataSource1'],
			multiSelect: true,
			cacheItems: true,
			'open-icon' : 'ace-icon tree-minus',
			'close-icon' : 'ace-icon tree-plus',
			'itemSelect' : true,
			'folderSelect': false,
			'selected-icon' : 'ace-icon fa fa-check',
			'unselected-icon' : 'ace-icon fa fa-times',
			loadingHTML : '<div class="tree-loading"><i class="ace-icon fa fa-refresh fa-spin blue"></i></div>'
		});
		
		$('#tree2').ace_tree({
			dataSource: sampleData['dataSource2'] ,
			multiSelect: true,
			cacheItems: true,
			'open-icon' : 'ace-icon tree-minus',
			'close-icon' : 'ace-icon tree-plus',
			'itemSelect' : true,
			'folderSelect': false,
			'selected-icon' : 'ace-icon fa fa-check',
			'unselected-icon' : 'ace-icon fa fa-times',
			loadingHTML : '<div class="tree-loading"><i class="ace-icon fa fa-refresh fa-spin blue"></i></div>'
		});

	var dataSave = []; //保存打钩的数据，用于显示
	//var initData = []; //初始化数据存放处，用于重置
	var dataPost =  {  //保存权限的更改，用于提交
		auth : [],
		revoke : []
	}; 
	var hadAdmin = false;
	var _comData = [];  //ajax请求得到的，当前管理员组件权限
	var _changeComData = [];

	//点击重置按钮
	$("#reset-btn").click(function(){
		dataSave = []; 
		//重新加载页面
		window.location.reload();
	});

	//点击提交按钮
	$("#submit-btn").click(function(){
		showBlackCover();
		
		$.ajax({
			type : "post",
			dataType : "json",
			url:  "<%=basePath %>/portal/saveUserComponents.do",
			data: {data:JSON.stringify(dataPost)},
			success : function(data){
				alert("数据提交成功");
				hideBlackCover();
				//重新加载页面
				windows.location.reload();
			},
			error : function(data){
				alert("数据提交失败");
				hideBlackCover();
			}
		});
	});

	//选择全部**********
		function setCheckAll(_this){
		$(_this).removeClass('unCheck');
		$(_this).addClass('hadCheck');
		$(_this).html("√");

		var _units = [];//数组
		var _unUnits = [];
	 	var _admin = $('#tree1').tree('selectedItems');
		//获取选择人员的uid
		var _uid = _admin[0].additionalParameters.uid;

		var _components = $('#tree2').tree('selectedItems');

	 	var hadAdmin = false;
		 //记录当前选择的人员
		 if(dataSave.length>0){
		 	for(var i=0; i<dataSave.length;i++){
		 		if(_uid==dataSave[i].uid){
		 			hadAdmin = true;
		 			break;
		 		}
		 	}
		 }
		 //当没有记录
		 if(!hadAdmin){
			 var json = {
			 	uid : _uid,
			 	units : []
			 }
			 dataSave.push(json);
		 }

		 // console.log(dataSave);
	 	//打钩已经授予的组件
		 $('#tree2 .tree-item .tree-label').each(function(){
		 	for(var i=0; i < _comArr.length; i++){
		 		if($(this).text()==_comArr[i].componentName){
		 			$(this).parent('.tree-item-name').parent('.tree-item').addClass('tree-selected');
		 			$(this).parent('.tree-item-name').children('.icon-item').removeClass('fa-times').addClass('fa-check');
		 		}
		 	}
		 });
		 //获取组件打钩的数据
		 for(var i=0; i<$('#tree2').tree('selectedItems').length;i++){
		 	_units[i] = $('#tree2').tree('selectedItems')[i];
		    //console.log(_units[i]);
		 }

	     //授权数据处理****************************************************************************************
		 var _diffent = true;	
		 var _hadPostComId = false;
		 var _hadAuthComId = false;
 	 	 var newRevoke = [];
 	 	 var _revokeSplice = false;
		 //如果打钩的和初始化数据ID不一样，则授权该组件
		 for(var i=0; i< _units.length; i++){
		 	_diffent = true;
		 	for(var j=0; j<_comData.length;j++){
		 		if(_comData[j].componentId==_units[i].additionalParameters.componentId){
		 			_diffent = false;
		 			break;
		 		}
		 	}
		 	if(_diffent){
		 		if(dataPost.auth.length>0){
		 			_hadPostComId = false;
			 		for(var k=0; k<dataPost.auth.length; k++){
			 			if(dataPost.auth[k].userId==_uid){
		 					if(dataPost.auth[k].componentId==_units[i].additionalParameters.componentId){
				 				_hadPostComId = true;
				 				break;
				 			}
			 			}
			 		
			 		}
		 		}
	 			if(!_hadPostComId||dataPost.auth.length==0){
	 				var json = {
			 			userId : _uid,
			 			componentId : _units[i].additionalParameters.componentId
			 		}
		 			dataPost.auth.push(json);
		 			//console.log(dataPost.auth);
		 		}
		 	}
		 }

		if(dataPost.revoke.length>0){
			for(var i=0; i<dataPost.revoke.length;i++){
				_hadAuthComId = false;
				 for(var j=0; j< _units.length; j++){
					if(_units[j].additionalParameters.componentId==dataPost.revoke[i].componentId){
			 			_hadAuthComId = true;
			 			break;
			 		}
				 }
		 		if(!_hadAuthComId){
		 			newRevoke.push(dataPost.revoke[i]);
		 		}
		 	}
		
		}

		for(var i=0;i<dataPost.revoke.length;i++){
 			if(dataPost.revoke[i].userId==_uid){
 				_revokeSplice = true;
	 			for(var j=0; j<newRevoke.length;j++){
 					if(dataPost.revoke[i].componentId==newRevoke[j].componentId){
 						_revokeSplice = false;
 						break;
 						
 					}
	 			}
		 		if(_revokeSplice){
		 			dataPost.revoke.splice(i,1);
		 			console.log('splice revoke');
		 		}
	 		}
	 	}
		for(var i=0;i<dataPost.revoke.length;i++){
 			if(dataPost.revoke[i].userId==_uid){
 				_revokeSplice = true;
	 			for(var j=0; j<newRevoke.length;j++){
 					if(dataPost.revoke[i].componentId==newRevoke[j].componentId){
 						_revokeSplice = false;
 						break;
 						
 					}
	 			}
		 		if(_revokeSplice){
		 			console.log("rI:"+i);
		 			dataPost.revoke.splice(i,1);
		 			console.log('splice revoke');
		 			i=0;
		 		}
	 		}
	 	}

		//dataPost.revoke = newRevoke;

	//撤销数据处理****************************************************************************************
		var _diffent2 = true;
		var _hadPostComId2 = false;
		var newAuth = [];
	 	var _hadRevokeComId = false;
	 	var _authSplice = false;

		 //如果没有打钩的和初始化数据ID一样，则撤销该组件
		for(var i=0; i<_comArr.length;i++){
			_diffent2 = true;
	 		 for(var j=0; j<_units.length;j++){
	 			if(_units[j].additionalParameters.componentId==_comArr[i].componentId){
	 				_diffent2 = false;
	 				break;
	 			}
	 		}
 		 	if(_diffent2){
				_unUnits.push(_comArr[i]); //保存没有打钩的数据
		 	}
		 }


	 	for(var i=0; i< _unUnits.length; i++){
	 		for(var j=0; j<_comData.length;j++){
	 			_hadPostComId2 = false;
	 			if(_comData[j].componentId==_unUnits[i].componentId){
	 				if(dataPost.revoke.length>0){
	 					for(var k=0; k<dataPost.revoke.length; k++){
							if(dataPost.revoke[k].userId==_uid){
		 						if(dataPost.revoke[k].componentId==_comData[j].componentId){
		 							_hadPostComId2 = true;
		 						}
		 					}
	 					}
	 					if(!_hadPostComId2){
			 				var json = {
					 			userId : _uid,
					 			componentId : _comData[j].componentId
					 		}
					 		dataPost.revoke.push(json);
	 					}
	 				}else{
 						var json = {
				 			userId : _uid,
				 			componentId : _comData[j].componentId
				 		}
				 		dataPost.revoke.push(json);
	 				}
		 		
	 			}
	 		}

	 	}

 		if(dataPost.auth.length>0){
	 		for(var i=0; i<dataPost.auth.length;i++){
				_hadRevokeComId = false;
			 	for(var j=0; j< _unUnits.length; j++){
			 		if(_unUnits[j].componentId==dataPost.auth[i].componentId){
	 					_hadRevokeComId = true;
	 					break;
	 				}
			 	}
	 		
	 			if(!_hadRevokeComId){
	 				newAuth.push(dataPost.auth[i]);
	 			}
	 		}
	 	
	 	}



	 	for(var i=0;i<dataPost.auth.length;i++){
 			if(dataPost.auth[i].userId==_uid){
 				_authSplice = true;
	 			for(var j=0; j<newAuth.length;j++){
 					if(dataPost.auth[i].componentId==newAuth[j].componentId){
 						_authSplice = false;
 						break;
 						
 					}
	 			}
		 		if(_authSplice){
		 			dataPost.auth.splice(i,1);
		 			console.log('splice auth');
		 		}
	 		}
	 	}

		//dataPost.auth = newAuth; 

	 	console.log(dataPost);
		 //保存组件打钩的数据，用于显示
	 	 if(dataSave.length>0){
		 	for(var i=0; i<dataSave.length;i++){
		 		if(_uid==dataSave[i].uid){
		 			dataSave[i].units = _units;		//临时保存对应管理员的权限
		 		//	console.log(dataSave[i]);
		 			break;
		 		}
		 	}
		 }
	}

	//取消全部***************************************
	function setCheckAllNot(_this){
		$(_this).addClass('unCheck');
		$(_this).removeClass('hadCheck');
		$(_this).html("");

		var _units = [];//数组
		var _unUnits = [];
	 	var _admin = $('#tree1').tree('selectedItems');
		//获取选择人员的uid
		var _uid = _admin[0].additionalParameters.uid;

		var _components = $('#tree2').tree('selectedItems');

	 	var hadAdmin = false;
		 //记录当前选择的人员
		 if(dataSave.length>0){
		 	for(var i=0; i<dataSave.length;i++){
		 		if(_uid==dataSave[i].uid){
		 			hadAdmin = true;
		 			break;
		 		}
		 	}
		 }
		 //当没有记录
		 if(!hadAdmin){
			 var json = {
			 	uid : _uid,
			 	units : []
			 }
			 dataSave.push(json);
		 }

	
		 // console.log(dataSave);
		 //清除所有打钩组件
		 $('#tree2 .tree-item').removeClass('tree-selected');
		 $('#tree2 .icon-item').addClass('fa-times').removeClass('fa-check');

		 //获取组件打钩的数据
		 for(var i=0; i<$('#tree2').tree('selectedItems').length;i++){
		 	_units[i] = $('#tree2').tree('selectedItems')[i];
		 }
		 //console.log(_units);

	     //授权数据处理****************************************************************************************
		 var _diffent = true;	
		 var _hadPostComId = false;
		 var _hadAuthComId = false;
 	 	 var newRevoke = [];
 	 	 var _revokeSplice = false;


		if(dataPost.revoke.length>0){
			for(var i=0; i<dataPost.revoke.length;i++){
	 			newRevoke.push(dataPost.revoke[i]);
		 	}
		
		}

		for(var i=0;i<dataPost.revoke.length;i++){
 			if(dataPost.revoke[i].userId==_uid){
 				_revokeSplice = true;
	 			for(var j=0; j<newRevoke.length;j++){
 					if(dataPost.revoke[i].componentId==newRevoke[j].componentId){
 						_revokeSplice = false;
 						break;
 						
 					}
	 			}
		 		if(_revokeSplice){
		 			console.log("rI:"+i);
		 			dataPost.revoke.splice(i,1);
		 			console.log('splice revoke');
		 			i=0;
		 		}
	 		}
	 	}

 	

		//dataPost.revoke = newRevoke;

	//撤销数据处理****************************************************************************************
		var _diffent2 = true;
		var _hadPostComId2 = false;
		var newAuth = [];
	 	var _hadRevokeComId = false;
	 	var _authSplice = false;

		 //如果没有打钩的和初始化数据ID一样，则撤销该组件

		for(var i=0; i<_comArr.length;i++){
			_unUnits.push(_comArr[i]); //保存没有打钩的数据
		 }

		console.log(_comData);

	 	for(var i=0; i< _unUnits.length; i++){
	 		for(var j=0; j<_comData.length;j++){
	 			_hadPostComId2 = false;
	 			if(_comData[j].componentId==_unUnits[i].componentId){
	 				if(dataPost.revoke.length>0){
	 					for(var k=0; k<dataPost.revoke.length; k++){
							if(dataPost.revoke[k].userId==_uid){
		 						if(dataPost.revoke[k].componentId==_comData[j].componentId){
		 							_hadPostComId2 = true;
		 						}
		 					}
	 					}
	 					if(!_hadPostComId2){
			 				var json = {
					 			userId : _uid,
					 			componentId : _comData[j].componentId
					 		}
					 		dataPost.revoke.push(json);
					 		 
	 					}
	 				}else{
 						var json = {
				 			userId : _uid,
				 			componentId : _comData[j].componentId
				 		}
				 		dataPost.revoke.push(json);
				 		
	 				}
		 		
	 			}
	 		}

	 	}

	 	//如果没有勾选和手动勾选授权ID相同，则不授权
 		if(dataPost.auth.length>0){
	 		for(var i=0; i<dataPost.auth.length;i++){
				_hadRevokeComId = false;
			 	for(var j=0; j< _unUnits.length; j++){
			 		if(_unUnits[j].componentId==dataPost.auth[i].componentId){
	 					_hadRevokeComId = true;
	 					break;
	 				}
			 	}
	 		
	 			if(!_hadRevokeComId){
	 				newAuth.push(dataPost.auth[i]);
	 			}
	 		}
	 	
	 	}
 	// console.log("unU"+_unUnits);
	// console.log("Auth:"+dataPost.auth);
	//  console.log("newAuth:"+newAuth);


	 	for(var i=0;i<dataPost.auth.length;i++){

 			if(dataPost.auth[i].userId==_uid){
 				_authSplice = true;
	 			for(var j=0; j<newAuth.length;j++){
 					if(dataPost.auth[i].componentId==newAuth[j].componentId){
 						_authSplice = false;
 						break;
 					}
	 			}
		 		if(_authSplice){
		 			
		 			dataPost.auth.splice(i,1);
		 			//console.log('splice auth len:'+dataPost.auth.length);
		 			i=0;
		 		}
	 		}
	 	}


	 	//重复，但是不能删！
		for(var i=0;i<dataPost.auth.length;i++){

	 			if(dataPost.auth[i].userId==_uid){
	 				_authSplice = true;
		 			for(var j=0; j<newAuth.length;j++){
	 					if(dataPost.auth[i].componentId==newAuth[j].componentId){
	 						_authSplice = false;
	 						break;
	 					}
		 			}
			 		if(_authSplice){
	 					console.log("aI:"+i);
			 			dataPost.auth.splice(i,1);
			 			//console.log('splice auth len:'+dataPost.auth.length);
			 			i=0;
			 		}
		 		}
		 	}


	 // console.log(dataPost.auth);
 	// 	 console.log(dataPost.revoke);

		//dataPost.auth = newAuth; 

	 	console.log(dataPost);
		 //保存组件打钩的数据，用于显示
	 	 if(dataSave.length>0){
		 	for(var i=0; i<dataSave.length;i++){
		 		if(_uid==dataSave[i].uid){
		 			dataSave[i].units = _units;		//临时保存对应管理员的权限
		 		//	console.log(dataSave[i]);
		 			break;
		 		}
		 	}
		 }
	}


	//点击全选按钮
	$(document).on('click','.checkAll',function(){
		if($(this).hasClass('unCheck')){
			setCheckAll(this);
		}else{
			setCheckAllNot(this);
		}
	});

	//选择人员
	$(document).on('click','#tree1 .tree-item',function(){

			 //展开组件
			 $('#tree2 .tree-branch').each(function(){
			 	if($(this).attr('role')=='treeitem'){
			 		if(!$(this).hasClass('tree-open')){
			 			$('#tree2').removeClass('hidden');
			 			$('#tree2 .icon-folder').trigger("click");
			 		}
			 	}
			 });

		    $('.sellectAll-btn').removeClass('hidden');

			//使选择管理员。只能单选
			$('#tree1 .tree-item').each(function(){
				$(this).removeClass('tree-selected');
				$(this).children('.tree-item-name').children('.icon-item').removeClass('fa-check').addClass('fa-times');
			});
			$(this).addClass('tree-selected');
			$(this).children('.tree-item-name').children('.icon-item').addClass('fa-check').removeClass('fa-times');
			 var _admin = $('#tree1').tree('selectedItems');
			 //获取选择人员的uid
			 var _uid = _admin[0].additionalParameters.uid;

		//ajax请求，post uid，获得组件数据。
			 //判断，当uid的data不为空时，改变
			//_comData = [{"componentId":"19"},{"componentId":"20"}];
			//_comData = [{"COMPONENT_ID":"19","USER_ID":"123"},{"COMPONENT_ID":"20","USER_ID":"123"},{"COMPONENT_ID":"21","USER_ID":"123"}];
			 	
		   	$.ajax({
				type : "post",
				dataType : "json",
				url:  "<%=basePath %>/portal/queryUserComponents.do",
				data: {
					userId : _uid,
				},
				success : function(data){
					if(data!=null){
						 console.log("获得数据！"); 
						
						_comData = delWithComId(data);
						_changeComData = _comData;
						renderAdminAndUnit();
					}else{
						alert.log("没有数据！");
					}
				},
				error : function(data){
					alert("数据加载失败！");
				}
			});

		 	//处理json格式
			function delWithComId(comData){
				
				var json = {};
				var jsonArr = [];
				console.log(comData.length);
				for(var i=0; i < comData.length; i++){
					
				    json = {
						componentId : comData[i].COMPONENT_ID
						//userId : _comData[i].USER_ID
					};
					jsonArr.push(json);
				}
				comData = jsonArr;
				return comData;
			}



			function renderAdminAndUnit(){
				 if(dataSave.length>0){
				 	for(var i=0; i<dataSave.length;i++){
				 		if(dataSave[i].uid==_uid){
				 			 var json = [];
				 			 for(var j=0; j<dataSave[i].units.length;j++){
				 			 	json.push(dataSave[i].units[j].additionalParameters);
				 			 }
				 			 _changeComData = json;
				 			 // console.log(json);
				 			 break;
				 		}
				 	}
				 }

				 //获得授予的组件名
				 var _comName = [];
				 for(var i=0; i<_changeComData.length;i++){
				 	for(var j=0; j<_comArr.length;j++){
				 		if(_changeComData[i].componentId==_comArr[j].componentId){
				 			_comName.push(_comArr[j].componentName);
				 			

				 		}
				 	}
				 }
				 //console.log(_comName);

				 //清除所有打钩组件
				 $('#tree2 .tree-item').removeClass('tree-selected');
				 $('#tree2 .icon-item').addClass('fa-times').removeClass('fa-check');
				//打钩已经授予的组件
				 $('#tree2 .tree-item .tree-label').each(function(){
				 	for(var i=0; i < _comName.length; i++){
				 		if($(this).text()==_comName[i]){
				 			$(this).parent('.tree-item-name').parent('.tree-item').addClass('tree-selected');
				 			$(this).parent('.tree-item-name').children('.icon-item').removeClass('fa-times').addClass('fa-check');
				 		}
				 	}
				 });
				 
			  	 if(_comName.length==_comArr.length){
				 	$('.checkAll').removeClass('unCheck');
					$('.checkAll').addClass('hadCheck');
					$('.checkAll').html("√");
				 }else{
		 			$('.checkAll').addClass('unCheck');
					$('.checkAll').removeClass('hadCheck');
					$('.checkAll').html("");
				 }
			}
		
			renderAdminAndUnit();

	});

	//选择组件
	$(document).on('click','#tree2 .tree-item',function(){

		var _units = [];//数组
		var _unUnits = [];
	 	var _admin = $('#tree1').tree('selectedItems');
		//获取选择人员的uid
		var _uid = _admin[0].additionalParameters.uid;

		var _components = $('#tree2').tree('selectedItems');

	 	var hadAdmin = false;
		 //记录当前选择的人员
		 if(dataSave.length>0){
		 	for(var i=0; i<dataSave.length;i++){
		 		if(_uid==dataSave[i].uid){
		 			hadAdmin = true;
		 			break;
		 		}
		 	}
		 }
		 //当没有记录
		 if(!hadAdmin){
			 var json = {
			 	uid : _uid,
			 	units : []
			 }
			 dataSave.push(json);
		 }

		 // console.log(dataSave);

		 //获取组件打钩的数据
		 for(var i=0; i<$('#tree2').tree('selectedItems').length;i++){
		 	_units[i] = $('#tree2').tree('selectedItems')[i];
		    //console.log(_units[i]);
		 }

	//授权数据处理****************************************************************************************
		 var _diffent = true;	
		 var _hadPostComId = false;
		 var _hadAuthComId = false;
 	 	 var newRevoke = [];
 	 	 var _revokeSplice = false;
		 //如果打钩的和初始化数据ID不一样，则授权该组件
		 for(var i=0; i< _units.length; i++){
		 	_diffent = true;
		 	for(var j=0; j<_comData.length;j++){
		 		if(_comData[j].componentId==_units[i].additionalParameters.componentId){
		 			_diffent = false;
		 			break;
		 		}
		 	}
		 	if(_diffent){
		 		if(dataPost.auth.length>0){
		 			_hadPostComId = false;
			 		for(var k=0; k<dataPost.auth.length; k++){
			 			if(dataPost.auth[k].userId==_uid){
		 					if(dataPost.auth[k].componentId==_units[i].additionalParameters.componentId){
				 				_hadPostComId = true;
				 				break;
				 			}
			 			}
			 		
			 		}
		 		}
	 			if(!_hadPostComId||dataPost.auth.length==0){
	 				var json = {
			 			userId : _uid,
			 			componentId : _units[i].additionalParameters.componentId
			 		}
		 			dataPost.auth.push(json);
		 			//console.log(dataPost.auth);
		 		}
		 	}
		 }

		if(dataPost.revoke.length>0){
			for(var i=0; i<dataPost.revoke.length;i++){
				_hadAuthComId = false;
				 for(var j=0; j< _units.length; j++){
					if(_units[j].additionalParameters.componentId==dataPost.revoke[i].componentId){
			 			_hadAuthComId = true;
			 			break;
			 		}
				 }
		 		if(!_hadAuthComId){
		 			newRevoke.push(dataPost.revoke[i]);
		 		}
		 	}
		
		}

		for(var i=0;i<dataPost.revoke.length;i++){
 			if(dataPost.revoke[i].userId==_uid){
 				_revokeSplice = true;
	 			for(var j=0; j<newRevoke.length;j++){
 					if(dataPost.revoke[i].componentId==newRevoke[j].componentId){
 						_revokeSplice = false;
 						break;
 						
 					}
	 			}
		 		if(_revokeSplice){
		 			dataPost.revoke.splice(i,1);
		 			console.log('splice revoke');
		 		}
	 		}
	 	}

		//dataPost.revoke = newRevoke;

	//撤销数据处理****************************************************************************************
		var _diffent2 = true;
		var _hadPostComId2 = false;
		var newAuth = [];
	 	var _hadRevokeComId = false;
	 	var _authSplice = false;

		 //如果没有打钩的和初始化数据ID一样，则撤销该组件
		for(var i=0; i<_comArr.length;i++){
			_diffent2 = true;
	 		 for(var j=0; j<_units.length;j++){
	 			if(_units[j].additionalParameters.componentId==_comArr[i].componentId){
	 				_diffent2 = false;
	 				break;
	 			}
	 		}
 		 	if(_diffent2){
				_unUnits.push(_comArr[i]); //保存没有打钩的数据
		 	}
		 }


	 	for(var i=0; i< _unUnits.length; i++){
	 		for(var j=0; j<_comData.length;j++){
	 			_hadPostComId2 = false;
	 			if(_comData[j].componentId==_unUnits[i].componentId){
	 				if(dataPost.revoke.length>0){
	 					for(var k=0; k<dataPost.revoke.length; k++){
							if(dataPost.revoke[k].userId==_uid){
		 						if(dataPost.revoke[k].componentId==_comData[j].componentId){
		 							_hadPostComId2 = true;
		 						}
		 					}
	 					}
	 					if(!_hadPostComId2){
			 				var json = {
					 			userId : _uid,
					 			componentId : _comData[j].componentId
					 		}
					 		dataPost.revoke.push(json);
	 					}
	 				}else{
 						var json = {
				 			userId : _uid,
				 			componentId : _comData[j].componentId
				 		}
				 		dataPost.revoke.push(json);
	 				}
		 		
	 			}
	 		}

	 	}

 		if(dataPost.auth.length>0){
	 		for(var i=0; i<dataPost.auth.length;i++){
				_hadRevokeComId = false;
			 	for(var j=0; j< _unUnits.length; j++){
			 		if(_unUnits[j].componentId==dataPost.auth[i].componentId){
	 					_hadRevokeComId = true;
	 					break;
	 				}
			 	}
	 		
	 			if(!_hadRevokeComId){
	 				newAuth.push(dataPost.auth[i]);
	 			}
	 		}
	 	
	 	}


	 	for(var i=0;i<dataPost.auth.length;i++){
 			if(dataPost.auth[i].userId==_uid){
 				_authSplice = true;
	 			for(var j=0; j<newAuth.length;j++){
 					if(dataPost.auth[i].componentId==newAuth[j].componentId){
 						_authSplice = false;
 						break;
 						
 					}
	 			}
		 		if(_authSplice){
		 			dataPost.auth.splice(i,1);
		 			console.log('splice auth');
		 		}
	 		}
	 	}

		//dataPost.auth = newAuth; 

	 	console.log(dataPost);
		 //保存组件打钩的数据，用于显示
	 	 if(dataSave.length>0){
		 	for(var i=0; i<dataSave.length;i++){
		 		if(_uid==dataSave[i].uid){
		 			dataSave[i].units = _units;		//临时保存对应管理员的权限
		 		//	console.log(dataSave[i]);
		 			break;
		 		}
		 	}
		 }
	
	});


	//渲染管理员数据
	function showUser(data){
				
		   var _datas = eval('(' + '${users}'+ ')');
			
			var tree_data = [];
			var _nodes = [];
			var _staffs = [];
			var treeNode = {};
			var treeStaff = {};

			for(var i=0; i < _datas.length; i++){
				_nodes[i] = _datas[i].node;
 				treeNode = {
					text: _nodes[i], 
					type: 'folder',
					additionalParameters : {
						//uid : "",
						children : {}
					}
				}

 				tree_data.push(treeNode);

				for(var j=0; j < _datas[i].staff.length; j++){
					_staffs[j] = _datas[i].staff[j];
					var _uid = _datas[i].staff[j].uid;
					treeStaff = {
						text: _staffs[j].givenname,
						type: 'item',
						additionalParameters :{
							uid : _uid
						}
					}
				
					//tree_data[i].additionalParameters.children[j] uid = _uid;
					tree_data[i].additionalParameters.children[j] = treeStaff;
				}
			}

			var dataSource1 = function(options, callback){
				var $data = null
				if(!("text" in options) && !("type" in options)){
					$data = tree_data;//the root tree
					callback({ data: $data });
					return;
				}
				else if("type" in options && options.type == "folder") {
					if("additionalParameters" in options && "children" in options.additionalParameters)
						$data = options.additionalParameters.children || {};
					else $data = {}//no data
				}
				
				// if($data != null)//this setTimeout is only for mimicking some random delay
				// 	setTimeout(function(){callback({ data: $data });} , parseInt(Math.random() * 500) + 200);
				if($data != null)
					callback({ data: $data });

				//we have used static data here
				//but you can retrieve your data dynamically from a server using ajax call
				//checkout examples/treeview.html and examples/treeview.js for more info
			}


		return dataSource1;
	}

	//渲染组件数据
	function showUnit(data){
		   	var _datas2 = eval('(' +'${componentTypes}'+ ')');
			_comArr = _datas2.component;
		
			//将数据转为数组形式
			var str = JSON.stringify(_datas2); 
			if(str.indexOf("[")!=0){
				str = "[" + str + "]";
				_datas2 = eval('(' + str + ')');
			}
		

			var tree_data2 = [];
			var _nodes2 = [];
			var _staffs2 = [];
			var treeNode2 = {};
			var treeStaff2 = {};


			for(var i=0; i < _datas2.length; i++){
				_nodes2[i] = _datas2[i].node;
				treeNode2 = {
					text: _nodes2[i], 
					type: 'folder',
					additionalParameters : {
						componentId : "",
						children : {}
					}
				}
		
				tree_data2.push(treeNode2);
	
				//console.log(tree_data2);

				for(var j=0; j < _datas2[i].component.length; j++){
					_staffs2[j] = _datas2[i].component[j];
					var _componentId = _datas2[i].component[j].componentId;
					treeStaff2 = {
						text: _staffs2[j].componentName,
						type: 'item',
						additionalParameters :{
							componentId : _componentId
						}
					}
				
					tree_data2[i].additionalParameters.children[j] = treeStaff2;
				}
			}

			var dataSource2 = function(options, callback){
				var $data = null
				if(!("text" in options) && !("type" in options)){
					$data = tree_data2;//the root tree
					callback({ data: $data });
					return;
				}
				else if("type" in options && options.type == "folder") {
					if("additionalParameters" in options && "children" in options.additionalParameters)
						$data = options.additionalParameters.children || {};
					else $data = {}//no data
				}
				
				// if($data != null)//this setTimeout is only for mimicking some random delay
					// setTimeout(function(){callback({ data: $data });} , parseInt(Math.random() * 500) + 200);
				if($data != null)//this setTimeout is only for mimicking some random delay
					callback({ data: $data });

				//we have used static data here
				//but you can retrieve your data dynamically from a server using ajax call
				//checkout examples/treeview.html and examples/treeview.js for more info
			}

			return dataSource2;

	}



	//初始化数据	
	function initiateDemoData(){
	
			var dataSource1 = showUser();
			var dataSource2 = showUnit();

			return {'dataSource1': dataSource1 , 'dataSource2' : dataSource2}

		}

});

		</script>
	</body>
</html>
